package com.affectu.search.fetch.thread;

import java.util.Date;

import org.hibernate.search.FullTextSession;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;

import com.affectu.common.base.UtilBase;
import com.affectu.common.util.PersistanceUtil;
import com.affectu.study.Paper;

/**
 * Oct 11, 2008
 * 
 * @author daniel nathan
 */
public abstract class PaperThread extends UtilBase implements Runnable,
		IFetchBase<Paper> {

	private String url;
	private String subPagePattern;

	public PaperThread(String url, String subPagePattern) {
		this.url = url;
		this.subPagePattern = subPagePattern;
	}

	public void run() {
		this.spidePage();
	}

	public void spidePage() {
		this.log("Fetch paper begin..." + url);

		try {
			Parser p = new Parser(url);
			p.setEncoding("gb2312");
			NodeList linkList = p.parse(new NodeClassFilter(LinkTag.class)); // 使用NodeClassFilter

			for (int i = 0; i < linkList.size(); i++) {// one level directory
				String subURL = ((LinkTag) linkList.elementAt(i)).getLink()
						.toString();
				if (subURL.indexOf(subPagePattern) != -1) {

					if (!this.checkLink(subURL)) {
						this.saveObject(this.spideSubPage(subURL));
					} else {
						this.log("already haved- url:" + url);// break;
					}
				}
			}

		} catch (Exception ex) {
			this.logAffectuException(ex, "取新闻异常:" + url);
		}

		this.log("Fetch paper end..." + url);
	}

	public abstract Paper spideSubPage(String url);

	public boolean saveObject(Paper paper) {
		lock.lock();
		boolean flag = false;
		FullTextSession em = PersistanceUtil.getInstance().getFullTextSession();
		try {			
			em.getTransaction().begin();
			paper.setId(System.currentTimeMillis());
			paper.setCreateDate(new Date());
			em.save(paper);
			em.getTransaction().commit();
			flag = true;
		} catch (Exception ex) {
			this.logAffectuException(ex, "存新闻异常");
		} finally {
			em.close();
			lock.unlock();
		}
		return flag;
	}

	public boolean checkLink(String link) {
		boolean flag = false;
		FullTextSession em = PersistanceUtil.getInstance().getFullTextSession();
		try {			
			Object ov = em.createSQLQuery(
					"select count(n.id) from Paper n where n.link=:link")
					.setParameter("link", link).uniqueResult();
			if (Integer.valueOf(ov.toString()).intValue() > 0) {
				flag = true;
			}
		} catch (Exception ex) {
			this.logAffectuException(ex, "检查link出错:" + link);
		}finally{
			em.close();
		}
		return flag;
	}
}
